<template>
  <div class="home">
    <!-- 左侧分类按钮 -->
    <div class="sidebar">
      <div
        class="category-btn"
        v-for="category in categories"
        :key="category.id"
        @click="fetchPosts(category.id)"
      >
        {{ category.name }}
      </div>
    </div>

    <!-- 右侧文章列表 -->
    <div class="content">
      <div v-if="isLoading" class="loading">加载中...</div>
      <div v-else>
        <div v-if="posts.length">
          <div v-for="post in posts" :key="post.ID" class="post-item">
            <!-- 文章左侧盒子，显示图片 -->
            <div class="post-image-box">
              <img v-if="post.attachment_url" :src="post.attachment_url" alt="Post Image" class="post-image" />
              <img v-else src="@/assets/logo.png" alt="Default Image" class="post-image" />
            </div>

            <!-- 文章内容部分 -->
            <div class="post-content">
              <h3>{{ post.title }}</h3>
              <p>{{ post.excerpt }}</p>
              <p class="author">发布者id：{{ post.user_id }}</p>
              <p class="date">发布时间：{{ formatDate(post.created_at) }}</p>
              <router-link v-if="post.ID" :to="`/article/detail/${post.ID}`" class="read-more">
                阅读全文
              </router-link>
            </div>
          </div>
        </div>
        <div v-else>
          <p class="no-data">暂无相关文章，请选择其他分类！</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import axios from 'axios';

export default {
  name: 'erjiFenlei',
  setup() {
    const categories = ref([
      { id: 0, name: 'GoLang' },
      { id: 1, name: 'Java' },
      { id: 2, name: 'Python' },
      { id: 3, name: 'Kotlin' },
    ]);
    const posts = ref([]);
    const isLoading = ref(false);

    const fetchPosts = async (categoryId) => {
      isLoading.value = true;
      const url = `http://localhost:9090/posts/categoryall/${categoryId}`;
      try {
        const response = await axios.get(url);
        // 假设后端返回的数据结构为 { data: [...] }
        posts.value = response.data.data || [];
      } catch (error) {
        console.error('获取文章失败:', error);
        posts.value = []; // 确保页面在错误时清空列表
      } finally {
        isLoading.value = false;
      }
    };

    // 格式化日期
    const formatDate = (dateString) => {
      const date = new Date(dateString);
      return date.toLocaleString(); // 或者根据需要自定义日期格式
    };

    // 默认加载第一个分类的文章
    onMounted(() => {
      if (categories.value.length) {
        fetchPosts(categories.value[0].id);
      }
    });

    return { categories, posts, fetchPosts, isLoading, formatDate };
  },
};
</script>

<style scoped>
/* 首页整体布局 */
.home {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  background-color: #f4f7fc;
}

/* 左侧分类按钮样式 */
.sidebar {
  width: 20%;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.category-btn {
  margin: 10px 0;
  padding: 12px;
  background-color: #42b983;
  color: white;
  font-size: 16px;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.category-btn:hover {
  background-color: #34a77b;
}

/* 右侧内容部分样式 */
.content {
  width: 75%;
  padding: 20px;
}

.post-item {
  display: flex;
  background-color: #fff;
  margin-bottom: 20px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
}

.post-image-box {
  width: 150px;
  height: 150px;
  margin-right: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f4f4f4;
  border: 1px solid #ddd;
}

.post-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.post-content {
  flex: 1;
}

.post-item h3 {
  font-size: 22px;
  color: #333;
  margin-bottom: 10px;
}

.post-item p {
  color: #666;
  font-size: 16px;
  margin-bottom: 15px;
}

.author {
  color: #999;
  font-size: 14px;
}

.date {
  color: #999;
  font-size: 14px;
}

.read-more {
  font-size: 14px;
  color: #42b983;
  text-decoration: none;
  font-weight: bold;
}

.read-more:hover {
  text-decoration: underline;
}

.loading {
  font-size: 18px;
  text-align: center;
  color: #666;
  padding: 50px 0;
}

.no-data {
  font-size: 16px;
  text-align: center;
  color: #999;
  margin-top: 20px;
}
</style>
